package application;
/**
 * This class is an inner shell. Which is used for basic communication. 
 * You can run it through another shell and operate its own shell.
 * @author Josef Vopalecky
 *
 */
public class Init extends Application {
	private int id;
	private Core core;
	
	/**
	 * Constructor.
	 * @param core
	 */
	public Init(Core core){
		this.core = core;
	}
	
	/**
	 * Set process ID.
	 * @param number
	 */
	public void setID(int number){
		id = number;
	}

	/**
	 * Login and init.
	 */
	@Override
	public void code() {
		while (!core.getGui().consoleIsRdy()) {
			try {//Console is not rdy. Wait.
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(core.canRead(id) == false){
			core.connectToGui(id);
		}
		core.getGui().writeConsole("Username: ");
		String user = core.read(id);
		core.getGui().writeConsole("Password: ");
		String password = core.read(id);
		while(!core.isEnd(id)){
			if(core.lastProccess(id)){
				if(core.canRead(id) == false){
					core.connectToGui(id);
				}
				else{
					String action = core.read(id);
					if(action != null && action != ""){
						core.runCommand(action,id);
					}
				}	
			}
			else{
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		core.closeIn(0);
		core.closeOut(0);
		core.cleanLastProccess();
		core.getGui().getOsGui().dispose();
		core.getGui().stop();
		core.stop();
	}
}
